import { DaHuaICCBaseService } from "./base";
import { dahua } from "./type";

/**
 * 大华ICC部门管理服务
 */
export class DepartmentService extends DaHuaICCBaseService {
	/**
	 * 新增部门
	 * @description 新增部门信息，支持设置上级部门、部门类型、负责人等信息
	 * @param data 部门信息
	 * @returns 返回新增的部门ID
	 */
	async addDepartment(data: dahua.icc.IAddDepartmentRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/department/add`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IAddDepartmentResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 更新部门
	 * @description 更新部门信息，支持修改名称、类型、负责人等信息，但不能修改部门ID
	 * @param data 部门更新信息
	 * @returns 返回更新的部门ID
	 */
	async updateDepartment(data: dahua.icc.IUpdateDepartmentRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/department/update`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IUpdateDepartmentResponse>>(url, {
			method: "put",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 批量新增部门
	 * @description 批量新增部门信息，需要预先获取部门全局ID
	 * @param data 批量新增参数
	 * @returns 返回新增的部门ID列表
	 */
	async batchAddDepartment(data: dahua.icc.IBatchAddDepartmentRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/department/batch-add`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IBatchAddDepartmentResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 删除部门
	 * @description 删除指定ID的部门信息
	 * @param data 删除参数
	 * @returns 返回被删除的部门ID
	 */
	async deleteDepartment(data: dahua.icc.IDeleteDepartmentRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/department/delete`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IDeleteDepartmentResponse>>(url, {
			method: "delete",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 查询部门详情
	 * @description 根据部门ID查询部门的详细信息
	 * @param id 部门ID
	 * @returns 返回部门详细信息
	 */
	async getDepartmentDetail(id: number) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/department/${id}`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IDepartmentDetailResponse>>(url, {
			method: "get",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 生成部门全局ID
	 * @description 生成部门全局ID，用于新增部门时使用
	 * @returns 返回生成的部门全局ID
	 */
	async generateDepartmentId() {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/department/generate-id`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IGenerateDepartmentIdResponse>>(url, {
			method: "get",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 批量生成部门全局ID
	 * @description 批量生成部门全局ID，用于批量新增部门时使用
	 * @param count 需要生成的部门全局ID数量
	 * @returns 返回生成的部门全局ID集合
	 */
	async batchGenerateDepartmentId(count: number) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/department/batch-generate-id?count=${count}`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IBatchGenerateDepartmentIdResponse>>(url, {
			method: "get",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 部门分页查询
	 * @description 分页查询部门信息，支持按上级部门ID、部门名称等条件筛选
	 * @param params 查询参数
	 * @returns 返回分页的部门信息
	 */
	async getDepartmentPage(params: dahua.icc.IDepartmentPageRequest = {}) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/department/page`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IDepartmentPageResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: params,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 部门全量数据同步
	 * @description 全量同步部门数据，支持分页和条件查询
	 * @param params 同步参数
	 * @returns 返回分页的部门信息
	 */
	async syncDepartmentList(params: dahua.icc.IDepartmentSyncRequest = {}) {
		const queryParams = new URLSearchParams();
		if (params.maxRangeId !== undefined) queryParams.append("maxRangeId", params.maxRangeId.toString());
		if (params.pageNum !== undefined) queryParams.append("pageNum", params.pageNum.toString());
		if (params.pageSize !== undefined) queryParams.append("pageSize", params.pageSize.toString());
		if (params.searchKey !== undefined) queryParams.append("searchKey", params.searchKey);

		const queryString = queryParams.toString();
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/department/list-page${queryString ? `?${queryString}` : ""}`;

		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IDepartmentPageResponse>>(url, {
			method: "get",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "form",
			dataType: "json",
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 部门树查询
	 * @description 查询部门树结构，支持按名称查询、展示人员节点等功能
	 * @param params 查询参数
	 * @returns 返回部门树数据
	 */
	async getDepartmentTree(params: dahua.icc.IDepartmentTreeRequest = {}) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/department/tree`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IDepartmentTreeResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: params,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 查询部门类型列表
	 * @description 查询部门类型列表，支持按启用状态筛选
	 * @param stat 启用状态；-1：全部，0：停用 1：启用
	 * @returns 返回部门类型列表数据
	 */
	async getDepartmentTypeList(stat: number = -1) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/department/type/${stat}`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IDepartmentTypeListResponse>>(url, {
			method: "get",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			dataType: "json",
		});
		this.handleResponseException(res);
		return res.data.data;
	}
}
